package com.xyht.sca_s.student_manage_system.modules.fileManagement.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.fileManagement.entity.req.SmsFileManagementReq;
import com.xyht.sca_s.student_manage_system.modules.fileManagement.service.SmsFileManagementService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 文件管理 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/fileManagement/")
public class SmsFileManagementController {
    @Autowired
    private SmsFileManagementService smsFileManagementService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "文件柜添加")
    @PostMapping("/add")
    public ResponseResult submitQuestionnaire(@RequestParam(value = "file", required = false) MultipartFile file,
                                              @RequestParam(value = "type") Integer type,
                                              @RequestParam(value = "name") String name,
                                              @RequestParam(value = "description", required = false) String description,
                                              @RequestParam(value = "p_id") String p_id,
                                              @RequestParam(value = "file_pic", required = false) String file_pic,
                                              HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsFileManagementService.addFileManagement(type, name, description, p_id, file_pic, user_id, file);
    }

    @ApiModelProperty(value = "文件下载")
    @GetMapping("/download")
    public ResponseResult downloadFileManagement(@RequestParam(value = "fileName") String fileName,
                                                 @RequestParam(value = "id") String id,
                                                 HttpServletResponse response) {
        return smsFileManagementService.downloadFileManagement(id, fileName, response);
    }

    @ApiModelProperty(value = "文件柜列表")
    @GetMapping("/list")
    public ResponseResult getFileManagementList(@RequestParam(value = "id",required = false) String id,
                                                @RequestParam(value = "searchStr", required = false) String searchStr,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return smsFileManagementService.getFileManagementList(id,searchStr, pageNum, pageSize);
    }

    @ApiModelProperty(value = "文件修改")
    @PostMapping("/update")
    @SecretBody
    public ResponseResult updateFileManagement(@RequestBody SmsFileManagementReq smsFileManagementReq) {
        return smsFileManagementService.updateFileManagement(smsFileManagementReq);
    }

    @ApiModelProperty(value = "文件删除")
    @PostMapping("/delete")
    @SecretBody
    public ResponseResult deleteFileManagement(@RequestBody SmsFileManagementReq smsFileManagementReq) {
        return smsFileManagementService.deleteFileManagement(smsFileManagementReq);
    }
}

